package cfs.model;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import org.mybeans.dao.DAOException;
import org.mybeans.dao.GenericDAO;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;

import cfs.databean.FundPriceHistory;

public class FundPriceHistoryDAO extends GenericDAO<FundPriceHistory> {
	public FundPriceHistoryDAO(GenericDAO<?>[] referencedBeanDAOs) {
		super(FundPriceHistory.class, "fund_price_history", new String[] {
				"fund_id", "price_date" }, referencedBeanDAOs);
		this.setIdleConnectionCleanup(true);
	}

	public ArrayList<FundPriceHistory> getLatestFundPriceList()
			throws DAOException {
		Date lastPricingDay = getLastPricingDay();
                ArrayList<FundPriceHistory> list = new ArrayList<FundPriceHistory>();
                if(lastPricingDay == null){
                    return list;
                }
		try {
			FundPriceHistory[] array = getFactory().match(
					MatchArg.equals("price_date", lastPricingDay));
			for (int i = 0; i < array.length; i++)
				list.add(array[i]);
			return list;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public Date getLastPricingDay() throws DAOException {
		try {
			FundPriceHistory[] list = getFactory().match(
					MatchArg.max("price_date"));
			if (list.length == 0)
				return null;
			else
				return list[0].getPrice_date();
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public FundPriceHistory getLatestFundByType(int fund_id)
			throws DAOException {
		Date lastPricingDay = getLastPricingDay();
		try {
			FundPriceHistory[] array = getFactory().match(
					MatchArg.equals("price_date", lastPricingDay),
					MatchArg.equals("fund_id", fund_id));

			if (array.length > 0)
				return array[0];
			else
				return null;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

	public ArrayList<FundPriceHistory> getFundByType(int fund_id)
			throws DAOException {
		FundPriceHistory[] array;
		try {
			array = getFactory().match(MatchArg.equals("fund_id", fund_id));
			ArrayList<FundPriceHistory> list = new ArrayList<FundPriceHistory>();
			for (int i = 0; i < array.length; i++)
				list.add(array[i]);
			return list;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}

	}

	public FundPriceHistory getLastestPrice(int fund_id) throws DAOException {
		try {
			FundPriceHistory[] historyList = this.getFactory().match(
					MatchArg.equals("fund_id", fund_id));
			if (historyList.length == 0)
				return null;
			Arrays.sort(historyList, new TimeComparator());
			return historyList[historyList.length - 1];
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}

}

class TimeComparator implements Comparator<FundPriceHistory> {
	public int compare(FundPriceHistory s1, FundPriceHistory s2) {
		long n1 = s1.getPrice_date().getTime();
		long n2 = s2.getPrice_date().getTime();
		if (n1 < n2)
			return -1;
		else if (n1 > n2)
			return 1;
		else
			return 0;
	}
};
